package com.boardour.log;

import android.util.Log;

/**
 * 日志打印工具类
 */
public class XLog {
    public static void v(Object... messages) {
        vt(null, messages);
    }

    public static void vt(String tag, Object... messages) {
        log(XLogType.V, tag, messages);
    }

    public static void d(Object... messages) {
        dt(null, messages);
    }

    public static void dt(String tag, Object... messages) {
        log(XLogType.D, tag, messages);
    }

    public static void i(Object... messages) {
        it(null, messages);
    }

    public static void it(String tag, Object... messages) {
        log(XLogType.I, tag, messages);
    }

    public static void w(Object... messages) {
        wt(null, messages);
    }

    public static void wt(String tag, Object... messages) {
        log(XLogType.W, tag, messages);
    }

    public static void e(Object... messages) {
        et(null, messages);
    }

    public static void et(String tag, Object... messages) {
        log(XLogType.E, tag, messages);
    }

    public static void a(Object... messages) {
        at(null, messages);
    }

    public static void at(String tag, Object... messages) {
        log(XLogType.A, tag, messages);
    }

    /**
     * 统一实现
     *
     * @param type     日志级别
     * @param tag      日志TAG
     * @param messages 打印内容
     */
    private static void log(@XLogType.Type int type, String tag, Object... messages) {
        XLogConfig config = XLogManager.getInstance().getLogConfig();
        if (config == null || !config.enable()) {
            return;
        }
        if (tag == null) {
            tag = config.tag();
        }
        // 解析内容
        String msg = parseMsg(messages);
        // 打印
        Log.println(type, tag, msg);
    }

    private static String parseMsg(Object[] messages) {
        StringBuilder sb = new StringBuilder();
        for (Object msg : messages) {
            if (msg == null) {
                continue;
            }
            sb.append(msg.toString()).append(";");
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }
}
